<?php
 
/*
 * Following code will delete a product from table
 * A product is identified by product id (pid)
 */
 
// array for JSON response
$response = array();
 
// check for required fields
if (isset($_POST['id']) && isset($_POST['unique_id'])) {
    $fid = $_POST['id'];
    $uid = $_POST['unique_id'];
 
    // include db connect class
    require_once __DIR__ . '/db_connect.php';
 
    // connecting to db
    $db = new DB_CONNECT();
    
    $user_id = mysql_query("SELECT USERS_id FROM MON_AN WHERE id = $fid");
    $row1 = mysql_fetch_array($user_id);
    $user_id = $row1['USERS_id'];
    //$response["id user"] = $user_id;
    
    $unique_id = mysql_query("SELECT unique_id FROM USERS WHERE id = $user_id");
    $row2 = mysql_fetch_array($unique_id);
    $unique_id = $row2['unique_id'];
    if($unique_id == $uid){
	    // mysql update row with matched pid
	    $result = mysql_query("DELETE FROM MON_AN WHERE id = $fid");
	    
	    // check if row deleted or not
	    if (mysql_affected_rows() > 0) {
	    	// successfully updated
	    	$response["success"] = 1;
	    	$response["message"] = "Food successfully deleted";
	    
	    	// echoing JSON response
	    	echo json_encode($response);
	    } else {
	    	// no product found
	    	$response["success"] = 0;
	    	$response["message"] = "No food found";
	    
	    	// echo no users JSON
	    	echo json_encode($response);
	    }
    } else {
    	$response["success"] = 2;
    	$response["message"] = "UID fail";
    	 
    	// echo no users JSON
    	echo json_encode($response);
    }
 
} else {
    // required field is missing
    $response["success"] = 3;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}
?>
